package com.liwang.lanouhn.nvshengo.fragments;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.liwang.lanouhn.nvshengo.Info.ContributeInfo;
import com.liwang.lanouhn.nvshengo.Info.VideoInfo;
import com.liwang.lanouhn.nvshengo.R;
import com.liwang.lanouhn.nvshengo.adapter.ContributeAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


/**
 * Created by lanouhn on 2016/10/24.
 */
public class WeekContribute extends Fragment {
    private RecyclerView rv_week;
    private List<ContributeInfo> infos = new ArrayList<>();
    private VideoInfo videoinfo;

    public static WeekContribute newInstants(VideoInfo info) {
        WeekContribute weekContribute = new WeekContribute();
        Bundle bundle = new Bundle();
        bundle.putSerializable("videoinfo", info);
        weekContribute.setArguments(bundle);
        return weekContribute;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_week, null);
        initView(view);
        getDataWeb();
        return view;
    }

    private void initView(View view) {
        rv_week = (RecyclerView) view.findViewById(R.id.rv_week);
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        rv_week.setLayoutManager(manager);
    }

    private void getDataWeb() {
        videoinfo = (VideoInfo) getArguments().getSerializable("videoinfo");
        String id = videoinfo.getId();
        final String url = "http://roomapicdn.plu.cn/ranklist/getweeklyranklist?roomId=" + id + "&version=3.7.0&device=4&packageId=1";
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().url(url).build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        try {
                            JSONArray array = new JSONArray(result);
                            for (int i = 0; i < array.length(); i++) {
                                JSONObject jsonObject = array.getJSONObject(i);
                                ContributeInfo info = new ContributeInfo();
                                info.setCount(jsonObject.getInt("count"));
                                info.setAvatar(jsonObject.getString("avatar"));
                                info.setUserName(jsonObject.getString("userName"));
                                infos.add(info);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        if (infos.size() > 0) {
                            handler.sendEmptyMessage(1);
                        } else {
                            handler.sendEmptyMessage(0);
                        }
                    }
                });

            }
        }).start();


    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Toast.makeText(getActivity(), "网络请求失败", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    ContributeAdapter adapter = new ContributeAdapter(getActivity(), infos);
                    rv_week.setAdapter(adapter);
                    break;

            }

        }
    };
}

